/**
 * Created by yangfan on 2016/8/29.
 */
define(["require",'config/appregister',"business/services/httpServices","i18n","business/services/utils",
    'business/services/moreHttpServices',
    "business/personMine/services/filter",
    "business/home/directives/share",
    "business/services/filter"
  ],function(require,app,httpService,i18n,utils,moreHttpServices){
    app.controller('techdetailCtrl',function($scope,$stateParams,$q,$compile,$anchorScroll,$location,$state,auth,$rootScope){
    	$scope.share = {
    		url:"www.baidu.com",
    		title:'你好啊',
    		summary:'我不是很好'
    	}
        $scope.$parent.isDetailPage = true;
        $scope.$parent.isMainpage = false;
        $scope.i18n = i18n;
        $scope.textarea ="";
        $scope.draw = {
            userPngSrc:"them/images/123.jpg",
            label:["别墅设计","施工图纸","建筑图纸"],
            setStyle:function(index){
                return {"border":"1px solid"+"red"}
            },
            content:{
                title:"内容简介:",
            },
            download:{
                fileType:"("+$scope.i18n.pdf+")",
                downloadNeed:5+"积分",
                uploadTime:"2016-7-1",
            },
            comment:{
                allComment:$scope.i18n.allComment ,
                commentNum:"("+132+")",
                placehoder:"写下你的评论......",
            }
        };
        $scope.enterEngDetail = function (item) {
            var state = {
                state:"eng.techdetail"
            };
            //localStorage.fileId = item.fileId;
            //localStorage.title = item.title;
            $state.go(state.state,{fileId:item.fileId},{reload:true});
        };

        function getLike (){
            var req = {
                type:"GET",
                data:{
                    fileId :fileId,
                    count:6,
                    userId:""
                }
            };
            httpService.getLike(req).success(function(response){
                if(!response.data) return;
                $scope.$apply(function(){
                    $scope.youLike = response.data;
                    if(response.data.length>5){
                        $scope.youLike = $scope.youlike.splice(0,5);
                    }
                })
            });
        };

        function getPreviewPngOfFile(){
            var req = {
                type: "GET",
                uri:'getPreviewPngOfFile',
                data:{
                    fileId:fileId,
                    type:1
                },
                removeCookie:false
            };
            httpService.getPreviewPng(req).success(function (response) {
                if (!response.data) {
                    return
                }
                $scope.$apply(function(){
                    $scope.getPreviewList = response.data.path.split('#');
                    $scope.getPreview = response.data.pageCount;
                })
            }).error(function () {
            })
        }
        $scope.dialogModel = {
            content:"想要查看更多内容请登录！",
            isShow:false,
            confirm:function(){
                $state.go("login",{type:"logion"})
            }
        }

        function getFile (){
            var req = {
                type:"GET",
                fileId :fileId
            };
            httpService.getFile(req).success(function(response){
                if(!response.data)return;
                $scope.$apply(function(){
                	  if(response.data.file.flag){
                        $scope.collectionTitle = "已收藏";
                        $scope.isCollected = true;
                    }else{
                        $scope.collectionTitle = "收藏";
                        $scope.isCollected = false;
                    }
                    $scope.navList = response.data.category ;
                    $scope.draws = response.data.file;
                    $scope.content = utils.contentConvert(response.data.file.introduction);
                    if(!!response.data.file.previewPath){
                        $scope.draws.file = 'http://"+allUrl+"/upload'  + response.data.file.previewPath;

                    }else{
                        $scope.draws.file = 'http://"+allUrl+"/upload/file'  + response.data.file.path;
                    }
                    $scope.draws.size = response.data.file.size ;
                    $scope.draws.downloadCount = response.data.file.downloadCount;
                    //$scope.shareCount = response.data.shareCount;
                    //$scope.share = response.data.share;
                    //var arr = response.data.share;
                      $scope.downnloadFile = response.data.file.fileId;
                    //if(!arr) return ;
                    //if(arr.length>6){
                    //    $scope.share = arr.slice(0,6)
                    //};
                });
            }).error(function(){
            });
        };

        // 共享资料
        function getShare(){
            var req = {
                type:"GET",
                fileId :fileId
            };
            httpService.getFileShare(req).success(function(response){
                $scope.$apply(function(){
                    $scope.share = response.data.share ;
                    if($scope.share.length > 7){
                        $scope.share = $scope.share.splice(0,7);
                    }
                    $scope.shareCount = response.data.shareCount ;
                })
            })
        }

        //获取下载排行数据
        function getDownLoadRank() {
            var req = {
                type: "GET",
                count: 6,
                data:{
                    mold:0,
                    tcId:2
                }

            };
            httpService.getDownLoadRank(req).success(function (response) {
                if (!response.data) {
                    return
                }
                $scope.downloadRank = response.data;
            }).error(function () {
            })
        };

        //获取一资分类
        function getRelevantType() {
            if (!$scope.fileId) {
                return
            };
            var req = {
                type: "GET",
                data: {
                    fileId: $scope.fileId,
                    count: 6
                }
            };
            httpService.getRelevantType(req).success(function (response) {
                if (!response.data) {
                    return
                }
                $scope.relevantType = response.data;
            }).error(function () {
            })
        };
        //获取相关推荐
        function getRelevant() {
            if (!$scope.fileId) {
                return
            };
            var req = {
                type: "GET",
                uri:"getRelevant",
                data: {
                    fileId: $scope.fileId,
                    count: 6
                }
            };
            httpService.getRelevant(req).success(function (response) {
                if (!response.data) {
                    return
                }
                $scope.relevant = response.data;
            }).error(function () {
            });
        };

        //更新收藏
        function updateCollection(){
            moreHttpServices.updateCollection($scope.fileId , 4).success(function(response){
               if(response.code == 0){
                   $scope.$apply(function(){
                       $scope.collectionTitle = "已收藏";
                       $scope.isCollected = true;
                   })
               }
                if(response.code == 112){
                    $scope.$apply(function(){
                        $scope.collectionTitle = "收藏";
                        $scope.isCollected = false;
                    })
                }
            }).error(function(){

            })
        }
        //初始化
        if( !angular.isUndefined($stateParams.fileId)){
            var deferred = $q.defer();
            var fileId = $stateParams.fileId;
            $scope.fileId=fileId;
                getFile ();
                getShare()
                getLike ();
            getPreviewPngOfFile();
               // getComment();
                getDownLoadRank();
                getRelevantType();
                getRelevant();
            $scope.commentConf = {
                showStar: false,
                conf: {
                    id: $stateParams.fileId,
                    type: 4
                }
            };
        };



        //获取面包树导航
        $scope.breadPath = ["工程资料库"];
        if(localStorage.scName && localStorage.dcName&& localStorage.title){
            $scope.breadPath.push(localStorage.scName);
            $scope.breadPath.push(localStorage.dcName);
            $scope.breadPath.push(localStorage.title);
        };

        $scope.hideAngle = function(index,arr){
            if(index == $scope.breadPath.length-1){
                return false
            }else {
                return true
            }
        };
        $scope.drawContent = $scope.draw.content.list;
        $scope.isCollected = false;
        $scope.collectionTitle = "收藏";
        $scope.updateCollection = function(){
           // $scope.collectionTitle = "收藏";
            //判断状态
            auth.isLogin().then(function(){
                updateCollection();
            })
        }
        $scope.goto =function(id){
            $("html,body").animate({scrollTop: $("#"+id)[0].offsetTop - 135}, 500);
        };
        $scope.stateGo =function(state,params){
            $state.go(state,params);
        };

        // 点击资料 显示下拉框
        $scope.isShowData = false;
        $scope.clickData = function(){
            if($scope.isShowData == false){
                $scope.isShowData = true;
            }
        };
        $scope.leaveSearchList =function(){
            if($scope.isShowData == true){
                $scope.isShowData = false;
            }
        };
        //详情
        $scope.goDetail = function(item){
            $state.go('eng.techdetail',{'fileId':item.fileId},{reload:true})
        };

        // 点击资料 显示下拉框
        $scope.isShowData = false;
        $scope.isInList = false;
        $scope.clickData = function(){
            if($scope.isShowData == false){
                $scope.isShowData = true;
            }
        };
        $scope.leaveSearchList =function(){
            if($scope.isShowData == true){
                $scope.isShowData = false;
            }
            $scope.isInList = true;
        };
        //三级里面没以及 所以传topItem
        $scope.searchMain = function(item,type,topItem){
            if(!type){
                return
            }
            var obj = {
                tcId:1,
                scId:"",
                dcId:""
            };
            if(type == "top"){
                obj.tcId = item.tcId;
            }
            if(type == "sec"){
                obj.scId = item.scId ;
                obj.tcId = item.tcId;
            }
            if(type == "detail"){
                obj.scId = item.scId ;
                obj.tcId = topItem.tcId;
                obj.dcId = item.dcId;
            }
            var str = JSON.stringify(obj);
            sessionStorage.engParams = str;
            $state.go('eng.main');
        }
        
        //下载资料
        $scope.download = function(id){
            auth.isLogin().then(function(){
                var URL = "http://"+allUrl+":8083";
                var API = "elink_database/api/file";
                var URI = "download";
                window.open(URL+"/"+API+"/"+URI+"?fileId="+id+"&seed="+ localStorage.seed + "&authToken=" + localStorage.authToken,"_blank");

            })
        };
        $scope.goToListPage = function(item){
            var obj = {
                tcId:$scope.draws.tcId,
                scId:item.scId,
                dcId:item.dcId
            };
            var str = JSON.stringify(obj);
            sessionStorage.engParams = str;
            $state.go('eng.main');
        }

        //点击标签 调至搜索模块
        $scope.jumpToSearchPage = function(item){
            $rootScope.GLOBALKEYWORD =  item;
            sessionStorage.searchKeyWord = $rootScope.GLOBALKEYWORD;
            $state.transitionTo('search.data', {}, {
                reload: true, inherit: false, notify: true
            });
        };
        $scope.jumpToUsersCenter = function(item){
            if(item.userId == $rootScope.userLoginInfo.data.userId){
                $state.go('myMainPage',{userId:item.userId})
            }else{
                $state.go('othersMainPage',{userId:item.userId})
            }
        }
        $scope.jumpToUpload = function(item){
            if(item.userId == $rootScope.userLoginInfo.data.userId){
                $state.go('prosonMine.myUpload')
            }else{
                $state.go('personOther.upload',{userId:item.userId})
            }

        }

        
    })
});
